﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SimpleServiceBus.Endpoints.ActiveMQ.Configuration;
using SimpleServiceBus.Endpoint;
using Sample.Messages;
using System.Diagnostics;

namespace SampleCommandLineProducer {
    class Program {
        static void Main(string[] args) {
            var _configurator = new ActiveMQConfigurator("SAMPLE_CMDLINE_PRODUCER", "CmdLineProducer")
                    .ConnectTo("failover:(tcp://localhost:61616)?timeout=3000")
                    .SetupDefaultConsumerPool(x => {
                        x.WorkerCount = 5;
                        //x.ConsumeQueue("SomeQueue");
                    })
                    .SetupMessageHandlers(x => {
                        x.Register(typeof(Program).Assembly);
                    })
                    .SetupMessageDestinations(x => {
                        x.Send<SimpleMessage>("CmdLineConsumer");
                    })
                    ;

            var _endpoint = _configurator.CreateEndpoint();
            _endpoint.Start();

            SendSimpleMessage(_endpoint);

            Console.WriteLine("Press any key to exit...");
            Console.ReadLine();
        }

        private static void SendSimpleMessage(IMessagingEndpoint endpoint) {
            var _count = 100;
            var _watch = new Stopwatch();
            for(Int32 i = 0; i < _count; i++) {
                Console.WriteLine(String.Format("Sending simple message #{0}", i));
                var _msg = new SimpleMessage { Value = i };
                _watch.Start();
                endpoint.MessageBus.Send(_msg);
                _watch.Stop();
            }
            var _messageRate = _watch.ElapsedMilliseconds >= 1000 ? _count / (_watch.ElapsedMilliseconds / 1000.0) : 0;
            Console.WriteLine(String.Format("Sent {0} messages in {1} ms => {2} messages / sec", _count, _watch.ElapsedMilliseconds, _messageRate));
        }

    }

}
